package com.manager.common.aspect;

import com.manager.common.annotation.AspectMethodOperation;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.utils.DataTools;
import com.manager.model.Dictionary_Data;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
@SuppressWarnings("all")
public class DictionaryAspect {

	@Pointcut("execution(public * com.manager.system.controller.DictionaryDataController.*(..))")
	private void controllerAspect() {
	}

	@Before(value = "controllerAspect()")
	public void methodBefore(JoinPoint joinPoint) throws Exception {

	}

	@Around(value = "controllerAspect()")
	public Object timeAround(ProceedingJoinPoint joinPoint) throws Exception {
		// 定义返回对象、得到方法需要的参数
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		Object obj = null;
		Object[] args = joinPoint.getArgs();
		long startTime = System.currentTimeMillis();
		boolean mark = true;
		try {
			obj = joinPoint.proceed(args);
		} catch (Throwable e) {
			e.printStackTrace();
			mark = false;
		}

		if (mark) {
			MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
			Method method = methodSignature.getMethod();
			AspectMethodOperation info = method.getAnnotation(AspectMethodOperation.class);
			if(info != null){
				Dictionary_Data dicData = (Dictionary_Data) joinPoint.getArgs()[0];
				synchronized (DataTools.dictionaryMap) {
					if (info.operation() == AspectMethodOperationEnum.ADD) {
						if (DataTools.dictionaryMap.containsKey(dicData.getGroupId())) {
							DataTools.dictionaryMap.get(dicData.getGroupId()).add(dicData);
						} else {
							List<Dictionary_Data> list = new ArrayList<Dictionary_Data>();
							list.add(dicData);
							DataTools.dictionaryMap.putIfAbsent(dicData.getGroupId(), list);
						}

					} else if (info.operation() == AspectMethodOperationEnum.UPDATE) {
						if (DataTools.dictionaryMap.containsKey(dicData.getGroupId())) {
							List<Dictionary_Data> list = DataTools.dictionaryMap.get(dicData.getGroupId());
							for (Dictionary_Data dic : list) {
								if (dic.getId().equals(dicData.getId())) {
									DataTools.dictionaryMap.get(dicData.getGroupId()).remove(dic);
									break;
								}
							}
							DataTools.dictionaryMap.get(dicData.getGroupId()).add(dicData);
						}
					} else if (info.operation() == AspectMethodOperationEnum.DELETE) {
						if (DataTools.dictionaryMap.containsKey(dicData.getGroupId())) {
							List<Dictionary_Data> list = DataTools.dictionaryMap.get(dicData.getGroupId());
							for (Dictionary_Data dic : list) {
								if (dic.getId().equals(dicData.getId())) {
									DataTools.dictionaryMap.get(dicData.getGroupId()).remove(dic);
									break;
								}
							}
						}
					}

				}
			}
			
		}

		return obj;
	}
}
